/**
 * OSS is a Open Service Container which gives the leverage of building the Desktop and Web
 * Application. * Copyright (C) 2008-2009  Ponraj Sutanthiramani
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Or5e Open Source Community
 * 105, S.S Paradise Apartment, Munnireddy Layout, Horamavu, Banaswadi, Bangalore, India.
 */
package org.or5e.api.webcam.images;

import java.awt.Rectangle;

public class PixelArray {

  public int width;
  public int height;
  public int[] pixels;
  public int[][] image;

  public PixelArray(int[] pixels, int width, int height) {
    this.pixels = pixels;
    this.height = height;
    this.width = width;
    image = null;
  }

  public int r(int x, int y) {
    try {
      return 0xFF & pixels[x + width * y];
    }
    catch(Exception e) {
      return 0;
    }
  }

  public static int r(int p) {
    return 0xFF & p;
  }

  public int grey(int x, int y) {
    try {
      int p = pixels[x + width * y];
      int tot = (0xFF & p);
      p >>= 8;
      tot += 0xFF & p;
      p >>= 8;
      tot += 0xFF & p;
      return tot/3;
    }
    catch(Exception e) {
      System.out.println(e);
      return 0;
    }
  }

  public byte[][] greyBytes() {
    byte[][] ba = new byte[width][height];
    for (int x=0; x<width; x++)
      for (int y=0; y<height; y++)
        ba[x][y] = (byte) grey(x, y);
    return ba;
  }

  public Rectangle getRect() {
    return new Rectangle(0, 0, width, height);
  }
}
